package com.mofum.cloud.core.model.impl;

import com.mofum.cloud.core.error.utils.IDException;
import com.mofum.cloud.core.model.Device;
import com.mofum.cloud.core.model.DeviceType;
import com.mofum.cloud.utils.IDUtils;

import java.math.BigInteger;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 标准设备
 * @author yumi@omuao.com
 */
public class StandardDeviceImpl implements Device {

    /**
     * 标准设备ID
     */
    private BigInteger id;

    /**
     * 设备名称
     */
    private String name;

    /**
     * 设备类型
     */
    private DeviceType deviceType;

    /**
     * 设备能力
     */
    private Map<String, Object> abilityMap;

    public StandardDeviceImpl() {
        this.init();
    }

    @Override
    public void init() {
        this.deviceType = DeviceType.UNKNOWN;
        try {
            id = IDUtils.getInstance().nextBigInteger();
        } catch (IDException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        this.abilityMap = new ConcurrentHashMap<>(10);
        this.name = id.toString();
    }

    @Override
    public BigInteger getId() {
        return id;
    }

    @Override
    public void setId(BigInteger id) {
        this.id = id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public DeviceType getDeviceType() {
        return deviceType;
    }

    @Override
    public void setDeviceType(DeviceType deviceType) {
        this.deviceType = deviceType;
    }

    @Override
    public Map<String, Object> getAbilityMap() {
        return abilityMap;
    }

    @Override
    public void setAbilityMap(Map<String, Object> abilityMap) {
        this.abilityMap = abilityMap;
    }

    @Override
    public void putAbility(String key, Object value) {
        this.getAbilityMap().put(key, value);
    }

    @Override
    public void removeAbility(String key) {
        this.getAbilityMap().remove(key);
    }

    @Override
    public Object getAbility(String key) {
        return this.getAbilityMap().get(key);
    }

    @Override
    public boolean containsAbility(String key) {
        return this.getAbilityMap().containsKey(key);
    }
}
